package com.example.zjhuojback.mapper;

import com.example.zjhuojback.entity.Problem;
import com.example.zjhuojback.entity.Submission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


@Mapper
public interface SubmissionMapper {

    @Insert("INSERT INTO `codesubmissions` (username, name, problemId, result, memoryUsage, timeUsage, language, submissionTime, code) " +
            "VALUES (#{username}, #{name}, #{problemId}, #{result}, #{memoryUsage}, #{timeUsage}, #{language}, #{submissionTime}, #{code})")
    void insert(Submission submission);

    @Select("<script>"
            + "SELECT * FROM `codesubmissions` "
            + "WHERE 1=1 "
            + "<if test='submission.problemId != null and submission.problemId != \"\"'>"
            + " AND `problemId` = #{submission.problemId} "
            + "</if>"
            + "<if test='submission.username != null and submission.username != \"\"'>"
            + " AND `username` = #{submission.username} "
            + "</if>"
            + "<if test='submission.language != null and submission.language != \"\"'>"
            + " AND `language` = #{submission.language} "
            + "</if>"
            + "<if test='submission.result != null and submission.result != \"\"'>"
            + " AND `result` = #{submission.result} "
            + "</if>"
            + "ORDER BY `submissionTime` DESC "
            + "LIMIT #{offset}, #{pageSize}"
            + "</script>")
    List<Submission> selectSubmissionsWithPagination(
            @Param("offset") int offset,
            @Param("pageSize") int pageSize,
            @Param("submission") Submission submission
    );


    @Select("SELECT COUNT(*) FROM `codesubmissions`")
    int getSubmissionCount();
}
